package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.ReasonCancellationLogic;

import data.ReasonCancellation;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class ReasonCancellationService extends HttpServlet{
	//http://localhost:8080/reasoncancellationService?func=addReasonCancellation&hotelId=1&title=1&description=aa
	//http://localhost:8080/reasoncancellationService?func=editReasonCancellation&reasoncancellationId=850&title=12345&description=aabbb
	//http://localhost:8080/reasoncancellationService?func=delReasonCancellation&reasoncancellationId=850
	//http://localhost:8080/reasoncancellationService?func=changeReasonCancellation&reasoncancellationId=850
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addReasonCancellation")){
				AddReasonCancellation(req,resp);
			}else if(doClass.equals("editReasonCancellation")){
				EditReasonCancellation(req,resp);
			}else if(doClass.equals("delReasonCancellation")){
				DeleteReasonCancellation(req,resp);
			}else if(doClass.equals("changeReasonCancellation")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getReasonCancellation4Admin")){
				GetReasonCancellation4Admin(req,resp);
			}else if(doClass.equals("getReasonCancellationById")){
				GetReasonCancellationById(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddReasonCancellation(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		String description = req.getParameter("description");

		new ReasonCancellationLogic().AddReasonCancellation(hotelId,title,description);

		GetReasonCancellation4Admin(req, resp);
	}

	private void EditReasonCancellation(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long reasoncancellationId = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("title");
		String description = req.getParameter("description");
				
		new ReasonCancellationLogic().EditReasonCancellation(reasoncancellationId,title,description);

		GetReasonCancellation4Admin(req, resp);

	}

	private void DeleteReasonCancellation(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String reasoncancellationId = req.getParameter("id");
		
		for(int i = 0; i < reasoncancellationId.split(",").length ; i++){
			new ReasonCancellationLogic().DeleteReasonCancellation(Long.parseLong(reasoncancellationId.split(",")[i]));
		}
		
		GetReasonCancellation4Admin(req, resp);
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long reasoncancellationId = Long.parseLong(req.getParameter("id"));

		Object obj = new ReasonCancellationLogic().ChangeStatus(reasoncancellationId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetReasonCancellation4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<ReasonCancellation> obj = new ReasonCancellationLogic().GetAllReasonCancellation4AdminLimit(hotelId,offset,line);
		double dataSize = new ReasonCancellationLogic().GetAllReasonCancellation4Admin(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetReasonCancellationById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		ReasonCancellation c = new ReasonCancellationLogic().GetReasonCancellation(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(c));
	}
}
